/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sg.edu.nus.iss.vmcs.system;

import sg.edu.nus.iss.vmcs.store.*;
import java.util.*;
import java.io.*;

/**
 *
 * @author LinnLinn
 */
public class FilePropertyLoaderImpl extends PropertyLoaderImpl{
    private static final String PROP_NUM_ITEMS = "NumOfItems";

	private Properties prop;
	private String fileName;
	
	// CashStoreItem // Linn	
	private static final String NAME_LABEL     = "Name";
	private static final String WEIGHT_LABEL   = "Weight";
	private static final String VALUE_LABEL    = "Value";
	private static final String QUANTITY_LABEL = "Quantity";
	
	//private static final String NAME_LABEL     = "Name";
	private static final String PRICE_LABEL    = "Price";
	//private static final String QUANTITY_LABEL = "Quantity";
	
	// DrinkStoreItem // Linn	
	public FilePropertyLoaderImpl(String fileName){
		this.fileName = fileName;
	}
	
	public void initialize(){
		try {
		prop = new Properties(System.getProperties());
		FileInputStream stream = new FileInputStream(fileName);
		prop.load(stream);
		stream.close();
		}
		catch(Exception ex){
			System.out.println("Error: " + ex.getMessage());
		}
	}
	
	public void saveProperty() throws IOException {
		FileOutputStream stream = new FileOutputStream(fileName);
		prop.store(stream, "");
		stream.close();
	}
	
	public int getNumOfItems() {
		String nm = prop.getProperty(PROP_NUM_ITEMS);
		int nmi;
		nmi = Integer.parseInt(nm);
		return nmi;
	}
	
	public void setNumOfItems(int vl) {
		prop.setProperty(PROP_NUM_ITEMS, String.valueOf(vl));
	}
	
	public StoreItem getItem(int index){
		
		if(this.fileName.equals(Environment.getCashPropFile())){ //Environment.CASH_PROP_FILE
		
			int idx = index + 1;
			Coin coin = new Coin();

			String name = new String(NAME_LABEL + idx);
			String value = getValue(name);
			coin.setName(value);

			name = new String(WEIGHT_LABEL + idx);
			value = getValue(name);
			coin.setWeight(Double.parseDouble(value));

			name = new String(VALUE_LABEL + idx);
			value = getValue(name);
			coin.setValue(Integer.parseInt(value));

			name = new String(QUANTITY_LABEL + idx);
			value = getValue(name);
			int qty = Integer.parseInt(value);

			CashStoreItem item = new CashStoreItem(coin, qty);
			return item;
		}
		else if(this.fileName.equals(Environment.getDrinkPropFile())){
			int idx = index + 1;
			DrinksBrand brand = new DrinksBrand();

			String name = new String(NAME_LABEL + idx);
			String value = getValue(name);
			brand.setName(value);

			name = new String(PRICE_LABEL + idx);
			value = getValue(name);
			brand.setPrice(Integer.parseInt(value));

			name = new String(QUANTITY_LABEL + idx);
			value = getValue(name);
			int qty = Integer.parseInt(value);

			DrinksStoreItem item = new DrinksStoreItem(brand, qty);
			return item;
		}
		else	
			return null;
	}

	public void setItem(int index, StoreItem sitem){
		if(this.fileName.equals(Environment.getCashPropFile())){ // Environment.CASH_PROP_FILE
			int idx = index + 1;

			CashStoreItem item = (CashStoreItem) sitem;
			Coin cn = (Coin) item.getContent();
			String itn = new String(NAME_LABEL + idx);

			setValue(itn, cn.getName());

			itn = new String(WEIGHT_LABEL + idx);
			setValue(itn, String.valueOf(cn.getWeight()));

			itn = new String(VALUE_LABEL + idx);
			setValue(itn, String.valueOf(cn.getValue()));

			itn = new String(QUANTITY_LABEL + idx);
			setValue(itn, String.valueOf(item.getQuantity()));
		}
		else if(this.fileName.equals(Environment.getDrinkPropFile())){
			int idx = index + 1;

			DrinksStoreItem dsItem = (DrinksStoreItem) sitem;
			DrinksBrand brand = (DrinksBrand) dsItem.getContent();
			String itn = new String(NAME_LABEL + idx);
			setValue(itn, brand.getName());

			itn = new String(PRICE_LABEL + idx);
			setValue(itn, String.valueOf(brand.getPrice()));

			itn = new String(QUANTITY_LABEL + idx);
			setValue(itn, String.valueOf(dsItem.getQuantity()));
		}
			
	}
 
	public String getValue(String key) {
		return prop.getProperty(key);
	}

	public void setValue(String key, String value) {
		prop.setProperty(key, value);
	}
}
